package cs342.project1.mathNotebook.xml;

import java.io.*;
import java.util.Collection;
import java.util.zip.GZIPOutputStream;

import cs342.project1.mathNotebook.notebook.NotebookEntry;


/**
 * Take outputStreams and writes compressed xml notebook entires to them
 * @author Gabrielle DuBois
 *
 */
public class CompressionWriter extends NotebookWriter {
    private XMLWriter xmlWrite;

	/**
     * @see cs342.project1.mathNotebook.xml.NotebookWriter#NotebookWriter(java.io.ByteArrayOutputStream)
     */
	public CompressionWriter (ByteArrayOutputStream outStream){
		super(outStream);
	}//constructor

    /**
     * @see cs342.project1.mathNotebook.xml.NotebookWriter#saveStream(java.util.Collection)
     */
    public ByteArrayOutputStream saveStream(final Collection<NotebookEntry> entries) {
		xmlWrite = new XMLWriter( new ByteArrayOutputStream() );
		GZIPOutputStream zipout;

		//uncompressed "file"
		byte[] xmlBytes = xmlWrite.saveStream(entries).toByteArray();
		xmlWrite.close();

		try {
			zipout = new GZIPOutputStream(out);
			zipout.write(xmlBytes, 0, xmlBytes.length);
			zipout.finish();
			zipout.close();
		} catch (IOException e) {
			System.out.println("CompressionWriter.saveStream() error: " + e);
		}//tryCatch

		return out;
    }//saveStream()

}//CompressionWriter
